import { http } from "@/utils/request";
import type { LinksResponse } from "./types";
// 获取分类列表
export const getLinksListAPI = (
  page: number,
  pageSize: number,
  token: string
) => {
  return http.get<LinksResponse>("/links", {
    params: {
      page: page,
      pageSize: pageSize,
    },
    headers: {
      Authorization: `Bearer ${token}`, // 在请求头中携带 token
    },
  });
};

// 添加分类
export const addLinksAPI = (
  title: string,
  href: string,
  categoryId: number,
  desc: string,
  token: string
) => {
  return http.post(
    "/links",
    {
      title: title,
      href: href,
      categoryId: categoryId,
      desc: desc,
    },
    {
      headers: { Authorization: `Bearer ${token}` },
    }
  );
};

// 删除分类
export const deleteLinksAPI = (id: number, token: string) => {
  return http.delete(`/links/${id}`, {
    headers: { Authorization: `Bearer ${token}` },
  });
};

// 编辑分类
export const editLinksAPI = (
  id: number,
  title: string,
  href: string,
  categoryId: number,
  desc: string,
  token: string
) => {
  return http.put(
    `/links/${id}`,
    {
      title: title,
      href: href,
      categoryId: categoryId,
      desc: desc,
    },
    {
      headers: { Authorization: `Bearer ${token}` },
    }
  );
};

// 查询分类
export const searchLinksAPI = (title: string, token: string) => {
  return http.get(`/links/${title}/search`, {
    headers: { Authorization: `Bearer ${token}` },
  });
};

// 上传图片
export const uploadImageAPI = (
  image: string,
  linkId: number,
  token: string
) => {
  return http.post(
    "/upload/link",
    { file: File, linkId: linkId },
    { headers: { Authorization: `Bearer ${token}` } }
  );
};
